<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名手机" v-model="form.username"> </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input placeholder="验证码" v-model="form.captcha">
        <template slot="append">
          <el-button @click="handleSendCaptcha"> 发送验证码 </el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input placeholder="你的名字" v-model="form.nickname"> </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPassword">
      <el-input
        placeholder="确认密码"
        type="password"
        v-model="form.checkPassword"
      >
      </el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">
      注册
    </el-button>
  </el-form>
</template>


<script>
import { sendCaptcha, register } from "./user";
export default {
  data() {
    const validateCheckPass = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入必填信息"));
      } else if (value != this.form.password) {
        callback(new Error("两次密码必须一致"));
      } else {
        callback();
      }
    };
    return {
      form: {
        username: "",
        nickname: "",
        captcha: "",
        password: "",
        checkPassword: "",
      },
      rules: {
        rules: {
          username: [
            {
              required: true,
              message: "请输入用户名",
              trigger: "blur",
            },
          ],
          nickname: [
            {
              required: true,
              message: "请输入必填信息",
              trigger: "blur",
            },
          ],
          captcha: [
            {
              required: true,
              message: "请输入必填信息",
              trigger: "blur",
            },
          ],
          password: [
            {
              required: true,
              message: "请输入必填信息",
              trigger: "blur",
            },
          ],
          checkPassword: [
            {
              validator: validateCheckPass,
              trigger: "blur",
            },
          ],
        },
      },
    };
  },
  methods: {
    handleSendCaptcha() {
      const regexp = /^1[3456789]\d{9}$/;
      if (!regexp.test(this.form.username)) {
        return this.$message.error("请输入正确手机号");
      }

      sendCaptcha(this.form.username).then((res) => {
        console.log(res.data);
        if (res.data.code) {
          this.$message.success("成功获取验证码:" + res.data.code);
        }
      });
    },
    handleRegSubmit() {
      console.log(this.form);
      this.$refs.form.validate().then((isValid) => {
        if (isValid) {
          const { checkPassword, ...data } = this.form;

          register(data).then((res) => {
            console.log(res.data);
            if (res) {
              // this.$store.commit("userstore/setUserInfo", res.data);
              this.$message.success("注册成功");
              // this.$router.push("/");
              this.$emit("toLogin");
            }
          });
        }
      });
    },
  },
};
</script>


<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>